# -*- coding: utf-8 -*-
# @Time    : 2024/2/4 20:11
# @Author  : micah
# @File    : 15.使用selenium完成邮箱登录.py
# @Software: PyCharm

import time
from selenium import webdriver
from selenium.webdriver.common.by import By


class LoginEmail:
    def __init__(self):
        self.driver = webdriver.Chrome()

    def open_email(self, url):
        self.driver.get(url)
        time.sleep(1)

    def login_email(self, email, password):
        # 1.定位到iframe标签的位置
        iframe = self.driver.find_element(By.XPATH, '//div[@id="loginDiv"]/iframe[@scrolling = "no"]')

        # 2.让selenium切入到这个子页面中
        self.driver.switch_to.frame(iframe)

        # 3.在指定的子页面中提取指定标签并输入账号密码信息
        self.driver.find_element(By.XPATH, '//input[@name="email"]').send_keys(email)
        self.driver.find_element(By.XPATH, '//div[@class="u-input box"]//input[@name="password"]').send_keys(password)

        # 4.点击登录按钮
        self.driver.find_element(By.XPATH, './/*[@id="dologin"]').click()

    def close(self):
        time.sleep(10)
        self.driver.quit()
        """
        close(): 关闭标签页, 标签页有多个的情况下只能关闭一个标签页
        quit(): 退出浏览器
        """


if __name__ == '__main__':
    login_email = LoginEmail()
    login_email.open_email('https://mail.163.com/')
    login_email.login_email('wt_poppies@163.com', 'wt199486')
    login_email.close()
